package com.qianfeng.xinyihezi.xinyihezi.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.Toast;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.fragment.ClassifyFragment;
import com.qianfeng.xinyihezi.xinyihezi.fragment.FirstFragment;
import com.qianfeng.xinyihezi.xinyihezi.fragment.GoodFragment;
import com.qianfeng.xinyihezi.xinyihezi.fragment.MeFragment;
import com.qianfeng.xinyihezi.xinyihezi.fragment.SubjectFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private RadioButton mButtonFirst, mButtonClassify, mButtonGood, mButtonSubject, mButtonMe;
    private List<Fragment> mFragment;
    private int currIndex, targetIndex;
    private FragmentManager mManager;
    private boolean isExit = false;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mManager = getSupportFragmentManager();
        initView();
        setListener();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_LONG).show();
                handler.sendEmptyMessageDelayed(0, 2000);
            } else {
                finish();
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void setListener() {
        mButtonFirst.setOnClickListener(this);
        mButtonClassify.setOnClickListener(this);
        mButtonGood.setOnClickListener(this);
        mButtonSubject.setOnClickListener(this);
        mButtonMe.setOnClickListener(this);
    }

    private void initView() {
        mFragment = new ArrayList<>();
        mFragment.add(new FirstFragment());
        mFragment.add(new ClassifyFragment());
        mFragment.add(new GoodFragment());
        mFragment.add(new SubjectFragment());
        mFragment.add(new MeFragment());
        FragmentTransaction transaction = mManager.beginTransaction();
        transaction.add(R.id.linear_main, mFragment.get(0));
        currIndex = 0;
        transaction.commit();
        mButtonFirst = (RadioButton) findViewById(R.id.rb_main_first);
        mButtonFirst.setChecked(true);
        mButtonClassify = (RadioButton) findViewById(R.id.rb_main_classify);
        mButtonGood = (RadioButton) findViewById(R.id.rb_main_good);
        mButtonSubject = (RadioButton) findViewById(R.id.rb_main_subject);
        mButtonMe = (RadioButton) findViewById(R.id.rb_main_me);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_main_first:
                targetIndex = 0;
                break;
            case R.id.rb_main_classify:
                targetIndex = 1;
                break;
            case R.id.rb_main_good:
                targetIndex = 2;
                break;
            case R.id.rb_main_subject:
                targetIndex = 3;
                break;
            case R.id.rb_main_me:
                targetIndex = 4;
                break;
        }
        chengeFragment();
    }

    private void chengeFragment() {
        if (currIndex == targetIndex) {
            Toast.makeText(this, "亲,这就是当前的内容", Toast.LENGTH_SHORT).show();
        } else {
            FragmentTransaction transaction = mManager.beginTransaction();
            Fragment tarFragment = mFragment.get(targetIndex);
            Fragment currFragment = mFragment.get(currIndex);
            if (tarFragment.isAdded()) {
                transaction.hide(currFragment);
                transaction.show(tarFragment);
            } else {
                transaction.hide(currFragment);
                transaction.add(R.id.linear_main, tarFragment);
            }
            currIndex = targetIndex;
            transaction.commit();
        }
    }
}
